package omm.domain.evolutiongraph;

import java.util.Map;
import java.util.TreeMap;

import org.joda.time.DateTime;
import org.joda.time.DateTimeComparator;

public class LastClosedMonthPeriodStrategy implements PeriodStrategy {

	final private Integer closedMonth;
	final private Integer year;
	final private Integer canvasWidth;
	final private DateTime end;
	
	public LastClosedMonthPeriodStrategy(int canvasWidth, DateTime end) {
		this.canvasWidth = canvasWidth;
		this.closedMonth = end.getMonthOfYear();
		this.year = end.getYear();
		this.end=end;
	}
	
	
	@Override
	public Map<DateTime, Double> calculatePeriods() {
		Map<DateTime, Double> periodsBars = new TreeMap<DateTime, Double>(
				DateTimeComparator.getDateOnlyInstance());
		
		DateTime firstDayOfMonth = new DateTime(year, closedMonth, 1, 0, 0, 0);
		periodsBars.put(firstDayOfMonth, 0.0);
		return periodsBars;
	}

	@Override
	public Integer calculateBarsWidth() {
		return (int) (canvasWidth / 4);
	}

	@Override
	public boolean isInPeriod(DateTime modifTime, DateTime dt) {
		return (modifTime.getMonthOfYear() == dt.getMonthOfYear() && modifTime.getYear() == dt.getYear());
	}

	@Override
	public String calculatePeriodsNames(Map<DateTime, Double> periodsValue) {
		String periodsNames = "";
		for (Map.Entry<DateTime, Double> e : periodsValue.entrySet()) {
			DateTime dt = e.getKey();
			Integer month = dt.getMonthOfYear();
			Integer year = dt.getYear();
			periodsNames = periodsNames + month + '/' + year.toString().substring(2) + '|';
			}
		return periodsNames.substring(0, periodsNames.length() - 1);
	}


	@Override
	public Integer calculateBarsSeparation() {
		return (canvasWidth - calculateBarsWidth()) / 2;
	}


	@Override
	public String getTitle() {
		return "del ultimo mes cerrado";
	}


	@Override
	public DateTime getBegin() {
		return end;
	}


	@Override
	public DateTime getEnd() {
		return end;
	}

}
